from typing import List

from fastapi import APIRouter, Request
from fastapi import Depends
from flask import jsonify

from config.get_db import get_db
from module_admin.entity.vo.expense_vo import ExpensesModel, ExpensePageObject, ExpensesModelForPage, \
    ExpensesModelForJisuan
from module_admin.entity.vo.user_vo import CurrentUserInfoServiceResponse, CasdoorUser
from module_admin.dao.dept_dao import *
from module_admin.service.expenses_service import ExpensesService
from module_admin.service.login_service import get_current_user, get_current_user_details
from utils.page_util import get_page_obj, get_new_page_obj
from utils.response_util import *
from utils.log_util import *

expensesController = APIRouter(
    dependencies=[Depends(get_current_user_details)]
)


@expensesController.post("/settings/create", response_model=ExpensesModel, summary="创建车辆收费设置")
async def create_expense(request: Request, expense_add: ExpensesModel, query_db: Session = Depends(get_db),
                         current_user: CasdoorUser = Depends(get_current_user_details)):
    try:
        expense_add.create_by = current_user.name
        expense_add.update_by = current_user.name
        result = ExpensesService.create_expense(query_db, expense_add)
        return response_200(data=result, message="新增成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@expensesController.get("/settings/delete/{expense_id}", summary="删除车辆收费设置")
async def delete_expense(request: Request, expense_id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = ExpensesService.delete_expense(query_db, expense_id)
        return response_200(data=dept_query_result, message="删除成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@expensesController.post("/settings/update", summary="修改车辆收费设置")
async def update_expense(request: Request, expense_update: ExpensesModel, db: Session = Depends(get_db)):
    try:
        dept_query_result = ExpensesService.update_expense(db, expense_update)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@expensesController.post("/settings/page", response_model=ExpensesModelForPage, summary="分页车辆收费设置")
async def page_expense(request: Request, expense_page_query: ExpensePageObject, db: Session = Depends(get_db)):
    try:
        page_result = ExpensesService.page(db, expense_page_query)
        total = ExpensesService.count(db, expense_page_query)
        list_format_datetime(page_result)
        # 分页操作
        page_result_after = get_new_page_obj(page_result, expense_page_query.page_num, expense_page_query.page_size,
                                             total)
        # 处理
        return response_200(data=page_result_after, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@expensesController.get("/settings/get/{expense_id}", response_model=ExpensesModelForJisuan,
                        summary="获取某个订单的费用")
async def delete_expense(request: Request, order_id: str, query_db: Session = Depends(get_db)):
    try:
        result = ExpensesService.get_expenses(query_db, order_id)
        return response_200(data=result, message="成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
